package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.can.CanIoTool;
import com.ruoyi.common.utils.modbus.Modbus4jUtils;
import com.ruoyi.common.utils.opcua.OpcUaServerUtil;
import com.ruoyi.quartz.service.ISysJobLogService;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Autowired
    private IMrBedService mrBedService;

    @Autowired
    private IMrDeviceService deviceService;

    @Autowired
    private IMrMonitorDevicePointValueService mrMonitorDevicePointValueService;

    @Autowired
    private ISysJobLogService jobLogService;

    @Autowired
    private ISysOperLogService operLogService;

    @Autowired
    private ISysLogininforService logininforService;

    @Resource
    private TaskExecutor taskExecutor;

    @Autowired
    private SensorDataService sensorDataService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IMrDeviceAlarmDataService mrDeviceAlarmDataService;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("定时加载缓存信息");
        mrBedService.loadingBedCache();
        deviceService.loadingCache();
    }

    /**
     * 设备数据上传任务
     *
     * @param host IP
     * @param port 端口
     */
    public void getDeviceData2(String host, String port) {
        if (sensorDataService.checkConnect()) {
            Modbus4jUtils.closeAllConnections();
            return;
        }
        taskExecutor.execute(() -> {
            mrMonitorDevicePointValueService.getDeviceData2(host, port);
        });
    }

    /**
     * 设备数据批量获取任务
     *
     * @param host IP
     * @param port 端口
     */
    public void getDeviceData3(String host, String port) {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskExecutor.execute(() -> {
            mrMonitorDevicePointValueService.getDeviceData3(host, port);
        });
    }


    /**
     * 设备数据输出任务
     */
    public void outputDeviceData() {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskExecutor.execute(() -> {
            taskService.outputDeviceData();
        });
    }

    /**
     * 清除设备上传数据任务
     */
    public void cleanDeviceData() {
        if (sensorDataService.checkConnect()) {
            return;
        }
        mrMonitorDevicePointValueService.cleanDeviceData();
        jobLogService.cleanJobLog();
        operLogService.cleanOperLog();
        logininforService.cleanLogininfor();
        mrDeviceAlarmDataService.deleteSixMonthsAgoData();
    }

    /**
     * 通讯监测任务
     */
    public void networkCheck() {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.networkCheck();
    }

    /**
     * 报警轮机员输出任务
     */
    public void marineEngineerTask() {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.marineEngineerTask();
    }

    /**
     * 信息化上报任务
     */
    public void informationReportingTask(String port, String slaveId) {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.informationReportingTask(Integer.parseInt(port), Integer.parseInt(slaveId));
    }

    /**
     * VDR上报任务
     */
    public void vdrReportingTask(String host, String port) {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.vdrReportingTask(host, port);
    }

    /**
     * UDP 协议上报任务
     */
    public void udpReportingTask(String host, String port, String dataSize) {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.udpReportingTask(host, port, dataSize);
    }

    /**
     * UDP 协议上报任务
     */
    public void udpReportingStringTask(String host, String port, String dataSize) {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.udpReportingStringTask(host, port, dataSize);
    }

    /**
     * UPC UA 协议客户端上报任务
     */
    public void upcuaReportingStringTask(String endpointUrl, String dataSize) {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.upcuaReportingStringTask(endpointUrl, dataSize);
    }

    /**
     * UPC UA 协议服务端上报任务
     */
    public void upcuaServerTask(String upcuaServerIp, String upcuaServerPort) {
        if (sensorDataService.checkConnect()) {
            OpcUaServerUtil.shutdownServer();
            return;
        }
        taskService.upcuaServerTask(upcuaServerIp, upcuaServerPort);
    }

    /**
     * CAN OPEN 协议任务
     */
    public void canopenTask() {
        if (sensorDataService.checkConnect()) {
            OpcUaServerUtil.shutdownServer();
            return;
        }
        taskService.canopenTask();
    }

    /**
     * CAN OPEN 协议输出任务
     */
    public void canopenOutputTask() {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskExecutor.execute(() -> {
            taskService.canopenOutputTask();
        });
    }

    /**
     * CAN 协议任务
     */
    public void canTask(String ip, int port, int moduleAddr, int type) {
        if (sensorDataService.checkConnect()) {
            CanIoTool tool = new CanIoTool(ip, port, moduleAddr);
            tool.close();
            return;
        }
        taskService.canTask(ip, port, moduleAddr, type);
    }

    /**
     * 数据库同步
     */
    public void dataSynchronization() {
        taskExecutor.execute(() -> {
            taskService.dataSynchronization();
        });
    }

    /**
     * 数据库一键同步
     */
    public void mySQLSyncAndUpdate() {
        taskExecutor.execute(() -> {
            taskService.mySQLSyncAndUpdate();
        });
    }

    /**
     * 水舱排放信息记录任务
     */
    public void infoSaveTask() {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.infoSaveTask();
    }

    /**
     * 燃油输送泵任务  fuelTransferPumpTask
     */
    public void fuelTransferPumpTask() {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.fuelTransferPumpTask();
    }

    /**
     * 水舱排放信息记录导出 任务
     */
    public void infoExportTask(String moon) {
        if (sensorDataService.checkConnect()) {
            return;
        }
        taskService.infoExportTask(moon);
    }

}
